package com.dc.server.vo;

import lombok.Data;

import java.util.concurrent.Delayed;
import java.util.concurrent.TimeUnit;

/**
 * @Auther: 赵聪
 * @Date: 2018/8/20 0020 22:02
 * @Description:
 */
@Data
public class ItemVo<T> implements Delayed {
    private long activeTime; //到期时间 单位毫秒
    private T data;//业务数据

    public ItemVo(long activeTime, T data) {

        this.activeTime = activeTime+System.currentTimeMillis();
        this.data = data;
    }

    /**
     *
     * 功能描述: 返回元素到激活日期的剩余时间
     *
     * @param:
     * @return:
     * @auther: zc
     * @date: 2018/8/20 0020 22:08
     */

    @Override
    public long getDelay(TimeUnit unit) {
        long convert = unit.convert(activeTime - System.currentTimeMillis(), unit);
        return convert;
    }

    @Override
    public int compareTo(Delayed o) {
        long d=getDelay(TimeUnit.NANOSECONDS)-o.getDelay(TimeUnit.NANOSECONDS);
        return (d==0)?0:(d>0?1:-1) ;
    }
}
